<?php

namespace app\common\model\marketing;

use addons\xyb\XypHelper;
use app\common\model\User;
use think\Model;


class AssociationUsers extends Model
{
    // 表名
    protected $name = 'association_users';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    protected $append = [
        'users_info'
    ];

    public function getUsersInfoAttr($value, $data){
        if(isset($data['user_id']) && $data['user_id']){
            return User::field('id,nickname,avatar')->where('id', $data['user_id'])->find();
        }
        return [];

    }
    public static function getUsersEnrollStatus($user_id, $association_id){
        $response =  [
            'execute_days' => 0,
            'complete_days' => 0,
            'surplus_days' => 0,
            'deadline_date' => '',
            'obtained_point' => 0,
            'reward_point' => 0,
            'grant_status' => false,
            'enroll_status' => false,
            'enroll_date' => '',
        ];
        $details= self::where('association_id', $association_id)->where('user_id', $user_id)->order('id desc')->find();
        if(!$details){
            return $response;
        }
        $response['enroll_status'] = true;
        $response['enroll_date'] = date('Y-m-d', $details->createtime);
        $response['execute_days'] = (int)$details->execute_days;
        $response['complete_days'] = (int)$details->complete_days;
        $response['surplus_days'] =  (int)($response['execute_days'] - $response['complete_days']);
        $response['deadline_date'] = date('Y-m-d', $details->deadline);
        $response['obtained_point'] = (int)$details->obtained_point;
        $response['reward_point'] = (int)$details->reward_point;
        $response['grant_status'] =  ($details['grant_status'] == 2 || $response['surplus_days'] < 1);
        return $response;
    }

    public static function getUsersJobsEnrollStatus($user_id, $association_id, $jobs_id){
        $response =  [
            'execute_days' => 0,
            'complete_days' => 0,
            'surplus_days' => 0,
            'deadline_date' => '',
            'obtained_point' => 0,
            'reward_point' => 0,
            'grant_status' => false,
            'enroll_status' => false,
            'enroll_date' => '',
        ];
        $details= self::where('association_id', $association_id)->where('jobs_id', $jobs_id)
            ->where('user_id', $user_id)->order('id desc')->find();
        if(!$details){
            return $response;
        }
        $response['enroll_status'] = true;
        $response['enroll_date'] = date('Y-m-d', $details->createtime);
        $response['execute_days'] = (int)$details->execute_days;
        $response['complete_days'] = (int)$details->complete_days;
        $response['surplus_days'] =  (int)($response['execute_days'] - $response['complete_days']);
        $response['deadline_date'] = date('Y-m-d', $details->deadline);
        $response['obtained_point'] = (int)$details->obtained_point;
        $response['reward_point'] = (int)$details->reward_point;
        $response['grant_status'] =  ($details['grant_status'] == 2 || $response['surplus_days'] < 1);
        return $response;
    }
    public static function getAssociationEnrollUsers($association_id){
        $field  = 'id,user_id,reward_point,grant_status';
        $association_users = AssociationUsers::field($field)
            ->where('association_id', $association_id)->select();
        if(!$association_users){
            return [];
        }
        return $association_users;
    }

    public function generateRandomNickname() {
        $adjectives = ['快乐的', '聪明的', '勇敢的', '懒惰的', '疯狂的', '温柔的'];
        $animals = ['狮子', '老虎', '熊', '兔子', '猫', '狗'];

        // 随机选择一个形容词和一个动物名
        $randomAdjective = $adjectives[array_rand($adjectives)];
        $randomAnimal = $animals[array_rand($animals)];

        // 组合并返回随机昵称
        return $randomAdjective . $randomAnimal;
    }

    function getRandomGravatarUrl($size = 150, $rating = 'g') {
        // 由于我们不需要实际的电子邮件地址来获取随机头像，
        // 我们可以使用一个固定的哈希值或者随机生成一个。
        $hash = md5('nonexistentemail@example.com'); // 或者使用 random_bytes() 生成一个随机哈希

        // 构建 Gravatar URL
        $url = "https://www.gravatar.com/avatar/$hash";
        $url .= "?s=$size&r=$rating&d=identicon";

        return $url;
    }
}
